package OBJ_Loader;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import klient.Klient;
import org.lwjgl.util.vector.Vector2f;
import org.lwjgl.util.vector.Vector3f;

/**
 *  
 * @author Marcin & Bartek
 * 
 * @description Imports Models from a file -> all of the methods are static
 * 
 */
public final class Loader {
    private Loader(){}
    public static Model load(File f){
        return load(f,0,0,0);    
    }
    public static Model load(File f,float xt,float yt,float zt){
        BufferedReader br=null;
        try{
        br = new BufferedReader(new FileReader(f));
        }catch(Exception e){
            Klient.log("Niepowodzenie: "+f.getPath());
            return null;
        }
        Klient.log("Otworzyłem "+f.getPath());
        Model m=new Model();
        String line;
        Material actM=null;
        int counter=0;
        try{
        while((line = br.readLine())!=null){
            ++counter;
            if(line.startsWith("mtllib ")){
                Klient.log("Ładuje: "+f.getPath().substring(0,f.getPath().lastIndexOf("\\")+1)+line.split(" ")[1]);
                m.setMtl(mtlLoader.load(f.getPath().substring(0,f.getPath().lastIndexOf("\\")+1)+line.split(" ")[1]));
            }else if(line.startsWith("usemtl ")){
                if(line.split(" ")[1].equals("None")) continue;
                actM=m.getMtl().get(line.split(" ")[1]);
            }else if(line.startsWith("v ")){
                m.getVert().add(new Vector3f(Float.valueOf(line.split(" ")[1])+xt,
                        Float.valueOf(line.split(" ")[2])+yt,
                        Float.valueOf(line.split(" ")[3])+zt));
            }else if(line.startsWith("vn ")){
                m.getNorm().add(new Vector3f(Float.valueOf(line.split(" ")[1]),
                        Float.valueOf(line.split(" ")[2]),
                        Float.valueOf(line.split(" ")[3])));
            }else if(line.startsWith("vt ")){
                m.getUv().add(new Vector2f(Float.valueOf(line.split(" ")[1]),
                        Float.valueOf(line.split(" ")[2])));
            
            }else if(line.startsWith("f ")){
               Vector3f vI = new Vector3f(Float.valueOf(line.split(" ")[1].split("/")[0]),
                       Float.valueOf(line.split(" ")[2].split("/")[0]),
                       Float.valueOf(line.split(" ")[3].split("/")[0]));
               Vector3f nI = new Vector3f(Float.valueOf(line.split(" ")[1].split("/")[2]),
                       Float.valueOf(line.split(" ")[2].split("/")[2]),
                       Float.valueOf(line.split(" ")[3].split("/")[2]));
               if(!line.split(" ")[1].split("/")[1].isEmpty()){
                   Vector3f tI = new Vector3f(Float.valueOf(line.split(" ")[1].split("/")[1]),
                       Float.valueOf(line.split(" ")[2].split("/")[1]),
                       Float.valueOf(line.split(" ")[3].split("/")[1])); 
                   m.getFace().add(new Face(vI,nI,tI,actM));
               }else
                   m.getFace().add(new Face(vI,nI));
            }
        }
        br.close();
        }catch(Exception e){
            Klient.log("Błąd pliku: "+f.getPath()+" w linii "+counter);
            return null;
        }    
        return m;
    }
}
